﻿using DLL;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.SBAR
{
    public partial class HistorySBAR : Form
    {
        #region 变量声明
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        Tb_User LoginUser;
        PatientInfo PatientInfo;
        #endregion

        #region 构造函数
        public HistorySBAR(Tb_User loginUser, PatientInfo patientInfo)
        {
            InitializeComponent();
            LoginUser = loginUser;
            PatientInfo = patientInfo;
            BindControl();
            BindInfo(this.dateE_StartTime.Text);
        }

        private void BindControl()
        {
            this.dateE_StartTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }
        #endregion

        #region 新增
        private void btnxinzeng_Click(object sender, EventArgs e)
        {
            AddSBAR pn = new AddSBAR(LoginUser, PatientInfo, string.Empty);
            pn.ShowDialog();
            string StatDate = this.dateE_StartTime.Text;
            if (pn.DialogResult == DialogResult.OK)
            {
                BindInfo(StatDate);
            }
        }
        #endregion

        #region 查询所有
        /// <summary>
        /// 依据日期区间查询
        /// </summary>
        /// <param name="dateE_StartTime"></param>
        /// <param name="dateE_EndTime"></param>
        public void BindInfo(string dateE_StartTime)
        {
            if (this.PatientInfo == null) return;
            string patientid = this.PatientInfo.PATIENT_ID;   //住院号
            int VisitId = this.PatientInfo.VISIT_ID;   //住院次数
            int FormId = (int)FormType.HandOverDuty;    //表单ID
            string AreaId = this.PatientInfo.NURSE_CELL_CODE; //病区ID
            DataTable dt = new DataTable();
            dt.Columns.Add("RiQi", typeof(string));
            dt.Columns.Add("ShiJian", typeof(string));
            dt.Columns.Add("PatientName", typeof(string));
            dt.Columns.Add("BedNo", typeof(string));
            dt.Columns.Add("SEX", typeof(string));
            dt.Columns.Add("PATIENT_ID", typeof(string));
            dt.Columns.Add("BingRenLieBie1", typeof(string));
            dt.Columns.Add("BingRenLieBie2", typeof(string));
            dt.Columns.Add("BingRenLieBie3", typeof(string));
            dt.Columns.Add("BingRenLieBie4", typeof(string));
            dt.Columns.Add("HuliJibie1", typeof(string));
            dt.Columns.Add("HuliJibie2", typeof(string));
            dt.Columns.Add("HuliJibie3", typeof(string));
            dt.Columns.Add("RuYuanZhenDuan", typeof(string));
            dt.Columns.Add("JiaoBanBingQingXianZhuang", typeof(string));
            dt.Columns.Add("JiaoBanBingQingBeiJing", typeof(string));
            dt.Columns.Add("JiaoBanBingQingPingGu", typeof(string));
            dt.Columns.Add("JiaoBanBingQingJianYi", typeof(string));
            dt.Columns.Add("JiaoBanRen", typeof(string));
            dt.Columns.Add("Id", typeof(string));
            var ret = formService.GetNursingFormRecordByDate(patientid, VisitId, FormId, Convert.ToDateTime(dateE_StartTime), Convert.ToDateTime(dateE_StartTime));
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderByDescending(p => p.AssessmentDate).ToList();
                if (!string.IsNullOrEmpty(AreaId))
                    list = list.Where(p => p.AreaId == AreaId).ToList();
                foreach (NursingFormRecord record in list)
                {
                    var NursingFormData = record.NursingFormData;
                    SBARHandOverDuty sbar = new SBARHandOverDuty();
                    sbar = XmlUtil.XMLToObject<SBARHandOverDuty>(NursingFormData, Encoding.UTF8);
                    DataRow newRow = dt.NewRow();
                    newRow["RiQi"] = Convert.ToDateTime(sbar.RiQi).ToString("yyyy-MM-dd");   //日期
                    newRow["ShiJian"] = sbar.ShiJian;   //时间
                    newRow["PatientName"] = record.PatientName;// PatientInfo.PATIENT_NAME;  //姓名
                    newRow["BedNo"] = record.BedNo;// PatientInfo.BED_NO; //床号
                    newRow["SEX"] = GetSex(record.PatientId);  //性别
                    newRow["PATIENT_ID"] = record.PatientId; //PatientInfo.PATIENT_ID; //病案号

                    //病人类别
                    string leibie = sbar.BingRenLieBie;
                    string[] LeiBieArray;
                    if (!string.IsNullOrEmpty(leibie))
                    {
                        LeiBieArray = leibie.Split('#');
                        if (Convert.ToInt32(LeiBieArray[0]) == 1)
                        {
                            newRow["BingRenLieBie1"] = "√";
                        }
                        if (Convert.ToInt32(LeiBieArray[1]) == 1)
                        {
                            newRow["BingRenLieBie2"] = "√";
                        }
                        if (Convert.ToInt32(LeiBieArray[2]) == 1)
                        {
                            newRow["BingRenLieBie3"] = "√";
                        }
                        if (Convert.ToInt32(LeiBieArray[3]) == 1)
                        {
                            newRow["BingRenLieBie4"] = "√";
                        }
                    }

                    //护理级别
                    string huliijibie = sbar.HuLiJiBie;
                    string[] JiBieArray;
                    if (!string.IsNullOrEmpty(huliijibie))
                    {
                        JiBieArray = huliijibie.Split('#');
                        if (Convert.ToInt32(JiBieArray[0]) == 1)
                        {
                            newRow["HuliJibie1"] = "√";
                        }
                        if (Convert.ToInt32(JiBieArray[1]) == 1)
                        {
                            newRow["HuliJibie2"] = "√";
                        }
                        if (Convert.ToInt32(JiBieArray[2]) == 1)
                        {
                            newRow["HuliJibie3"] = "√";
                        }
                    }

                    newRow["RuYuanZhenDuan"] = sbar.RuYuanZhenDuan; //入院诊断
                    newRow["JiaoBanBingQingXianZhuang"] = sbar.JiaoBanBingQingXianZhuang;    //现状
                    newRow["JiaoBanBingQingBeiJing"] = sbar.JiaoBanBingQingBeiJing;  //背景
                    newRow["JiaoBanBingQingPingGu"] = sbar.JiaoBanBingQingPingGu; //评估
                    newRow["JiaoBanBingQingJianYi"] = sbar.JiaoBanBingQingJianYi; //建议
                    newRow["JiaoBanRen"] = sbar.JiaoBanRen;    //签名
                    //ID隐藏，双击触发事件
                    newRow["Id"] = record.Id;
                    dt.Rows.Add(newRow);
                }
            }
            dt.DefaultView.Sort = "RiQi DESC,ShiJian DESC";
            DataTable data = dt.DefaultView.ToTable();
            gridControl1.DataSource = data;

            //总人数
            if (string.IsNullOrEmpty(this.dateE_StartTime.Text)) return;
            if (dt.Rows.Count == 0) return;
            string sql = string.Format("SELECT TOP 1 NursingFormData FROM NursingPublicRelation(NOLOCK) WHERE FormId='{0}' AND  AreaId='{1}' AND AssessmentDate='{2}'", FormId, AreaId, this.dateE_StartTime.Text);
            object obj = SqlHelper.ExecuteScalar(sql);
            if (obj != DBNull.Value && obj != null && !string.IsNullOrEmpty(obj.ToString()))
            {
                var NursingFormData1 = obj.ToString();
                SBARNursingRelation NR = new SBARNursingRelation();
                NR = XmlUtil.XMLToObject<SBARNursingRelation>(NursingFormData1, Encoding.UTF8);
                txtbingrenzongshu.Text = NR.BingRenZongshu;
                txtxinruyuan.Text = NR.XinRuYuan;
                txtchuyuan.Text = NR.ChuYuan;
                txtzhuanru.Text = NR.ZhuanRu;
                txtzhuanchu.Text = NR.ZhuanChu;
                txtsiwang.Text = NR.SiWang;
                txtyijibingren.Text = NR.YiJiBingRen;
            }
        }
        #endregion

        #region 查询
        private void btnchaxun_Click(object sender, EventArgs e)
        {
            string StatDate = this.dateE_StartTime.Text;
            if (string.IsNullOrEmpty(StatDate))
            {
                MessageBox.Show("查询日期不能为空.");
                return;
            }
            txtbingrenzongshu.Text = string.Empty;
            txtxinruyuan.Text = string.Empty;
            txtchuyuan.Text = string.Empty;
            txtzhuanru.Text = string.Empty;
            txtzhuanchu.Text = string.Empty;
            txtsiwang.Text = string.Empty;
            txtyijibingren.Text = string.Empty;
            BindInfo(StatDate);
        }
        #endregion

        #region 修改
        private void Btnxiugai_Click(object sender, EventArgs e)
        {
            ShowEditForm();
        }
        #endregion

        #region 删除
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount <= 0) return;
            if (MessageBox.Show("确认要删除数据吗？", "删除前确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                string id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                var ret2 = formService.DeleteNursingFormRecord(id);
                MessageBox.Show(ret2.M);
                if (ret2.Code == 1)
                {
                    string StatDate = this.dateE_StartTime.Text;
                    BindInfo(StatDate);
                }
            }
        }
        #endregion

        #region 打印
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            DateTime Startdate = Convert.ToDateTime(this.dateE_StartTime.Text);
            PrintSBAR pp = new PrintSBAR(PatientInfo, Startdate);
            pp.Show();
        }
        #endregion

        private void bandedGridView1_DoubleClick(object sender, EventArgs e)
        {
            ShowEditForm();
        }

        private void ShowEditForm()
        {
            if (bandedGridView1.DataRowCount <= 0) return;
            string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
            AddSBAR add = new AddSBAR(LoginUser, PatientInfo, Id);
            add.ShowDialog();
            if (add.DialogResult != DialogResult.OK) return;
            string StatDate = this.dateE_StartTime.Text;
            BindInfo(StatDate);
        }

        //获取患者性别
        private object GetSex(string patientId)
        {
            string sqlStr = string.Format("SELECT TOP 1 SEX FROM V_YDHL_PATIENT(NOLOCK) WHERE PATIENT_ID='{0}' ", patientId);
            return SqlHelper.ExecuteScalar(sqlStr);
        }
    }
}
